//
//  LBCategoryPullView.m
//  AFNetworking
//
//  Created by luckyRoy on 2018/2/2.
//

#import "LBCategoryPullView.h"
#import "LBCategoryMainView.h"
#import "Masonry.h"
#import "LBCategoryManager.h"
#import "LBLoadingView.h"
#import "LBAlert.h"
#import "LBCategorySettingModel.h"
#import "UIApplication+LBViewController.h"

static CGFloat const LBCategoryPullViewMinHeight = 50;

@interface LBCategoryPullView ()

@property (nonatomic, strong) UIControl *blackBgView;

@property (nonatomic, strong) LBCategoryMainView *categoryView;

@property (nonatomic, copy) LBNewCategoryMainViewChose clickBlock;

@property (nonatomic, strong) NSMutableArray *selectedCodeArray;

@property (nonatomic, strong) LBCategorySettingModel *settingModel;

@property (nonatomic, strong) UIViewController *beginViewController;

@end

@implementation LBCategoryPullView

- (instancetype)initWithFrame:(CGRect)frame
                 categoryCode:(NSString *)selectedCategoryCode
                     codeType:(LBCategoryBizCodeType)codeType
             maxSelectedLevel:(NSInteger)level
                     complete:(LBNewCategoryMainViewChose)complete
{
    if (self = [super initWithFrame:frame]) {
        self.settingModel.codeType = codeType;
        self.settingModel.maxLevel = level;
        self.clickBlock = complete;
        [self.selectedCodeArray removeAllObjects];
        if (selectedCategoryCode.length) {
            [self.selectedCodeArray addObject:selectedCategoryCode];
        }
                
        [self setBeginViewController];
        
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
    [self addSubview:self.blackBgView];
    
    [self.blackBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self addSubview:self.categoryView];
    
    self.categoryView.frame = CGRectMake(0, 0,CGRectGetWidth(self.frame), LBCategoryPullViewMinHeight);
    
    [self setHidden:YES];
    
    [self requestForListModel];
}

- (void)setBeginViewController
{
    UINavigationController *currentNavViewController = [UIApplication getCurrentViewController].navigationController;
    
    if (!currentNavViewController) {
        currentNavViewController = [UIApplication sharedApplication].keyWindow.rootViewController ;
    }
    
    self.beginViewController = [currentNavViewController.viewControllers lastObject];
}

- (UINavigationController *)getNavigationController
{
    UINavigationController *currentNavViewController = [UIApplication getCurrentViewController].navigationController;
    
    if (!currentNavViewController) {
        currentNavViewController = [UIApplication sharedApplication].keyWindow.rootViewController ;
    }
    
    return currentNavViewController;
}

#pragma mark - click

- (void)show
{
    CGFloat maxPullHeight = [[UIScreen mainScreen] bounds].size.height - 180;
    [self setHidden:NO];
    [self reloadListView];
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.blackBgView.backgroundColor = [[UIColor darkGrayColor]colorWithAlphaComponent:0.4];
        CGRect rect = self.categoryView.frame;
        rect.size.height = maxPullHeight;
        self.categoryView.frame = rect;
    } completion:^(BOOL finished) {
        self.isShowPullView = YES;
    }];
}

- (void)dimiss
{
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.blackBgView.backgroundColor = [[UIColor darkGrayColor]colorWithAlphaComponent:0];
        CGRect rect = self.categoryView.frame;
        rect.size.height = LBCategoryPullViewMinHeight;
        self.categoryView.frame = rect;
    } completion:^(BOOL finished) {
        [self setHidden:YES];
        self.isShowPullView = NO;
    }];
}

#pragma mark - request

- (void)requestForListModel
{
    __weak __typeof(&*self)weakSelf = self;
//    [LBLoadingView showInView:self];
    [LBCategoryManager getCategoryListByBizCode:self.settingModel.codeType Depth:@(self.settingModel.maxLevel) Success:^(NSArray<LBCategoryModel *> *modelArray) {
        [LBLoadingView dismiss];
        [weakSelf refreshDataByArray:modelArray];
        [weakSelf reloadListView];
    } fail:^(NSString *errMsg) {
        [LBLoadingView dismiss];
        [LBAlert say:errMsg];
    }];
}

- (void)refreshDataByArray:(NSArray *)modelArray
{
    __weak __typeof(&*self)weakSelf = self;
    weakSelf.settingModel.selectedModelArray = [LBCategorySettingModel selectedModelArrayFromCodeArray:weakSelf.selectedCodeArray totalArray:modelArray isSingleSelected:weakSelf.settingModel.isSingleSelected];
    [weakSelf.settingModel setOriginalArrayByData:[weakSelf.settingModel getOriginalArrayByOriginalData:modelArray]];
}

- (void)reloadListView
{
    [self.categoryView reloadViewByNewListModels:[LBCategorySettingModel getListModelBySettingModel:self.settingModel currentPage:LBCategorySettingPageTypeHomePage]];
}

#pragma mark - setter

#pragma mark - getter

- (UIControl *)blackBgView
{
    if (!_blackBgView) {
        _blackBgView = [[UIControl alloc]init];
        _blackBgView.backgroundColor = [[UIColor darkGrayColor]colorWithAlphaComponent:0];
        [_blackBgView addTarget:self action:@selector(dimiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _blackBgView;
}

- (LBCategoryMainView *)categoryView
{
    if (!_categoryView) {
        LBCategoryMainViewLayout *viewLayout = [LBCategoryMainViewLayout new];
        viewLayout.isShowLookingAllView = YES;
        __weak __typeof(&*self)weakSelf = self;
        _categoryView = [[LBCategoryMainView alloc]initWithFrame:self.bounds settingModel:self.settingModel viewLayout:viewLayout complete:^(LBCategoryMainViewLayout *blockModel,NSMutableArray *selectedArray) {
            if (weakSelf.beginViewController) {
                [[weakSelf getNavigationController]popToViewController:weakSelf.beginViewController animated:NO];
            }
            id parm = nil;
            if (blockModel.isLookingAll) {
                //查看全部
                parm = @(YES);
            }
            if (weakSelf.clickBlock) {
                weakSelf.clickBlock(weakSelf.settingModel.selectedModelArray,parm);
            }
            [weakSelf dimiss];
        }];
        [_categoryView setBackgroundColor:[UIColor whiteColor]];
    }
    return _categoryView;
}

- (LBCategorySettingModel *)settingModel
{
    if (!_settingModel) {
        _settingModel = [[LBCategorySettingModel alloc]init];
        //默认单选
        _settingModel.isSingleSelected = YES;
        //默认选中全部
        _settingModel.isShowAll = YES;
    }
    return _settingModel;
}

- (NSMutableArray *)selectedCodeArray
{
    if (!_selectedCodeArray) {
        _selectedCodeArray = [[NSMutableArray alloc]init];
    }
    return _selectedCodeArray;;
}

+ (CGFloat)fac_heightForViewByModel:(id)viewModel
{
    CGFloat totalHeight = 0;
    
    return totalHeight;
}


@end
